package com.dz.controller.user;

import com.dz.constant.MessageConstant;
import com.dz.context.BaseContext;
import com.dz.entity.AddressBook;
import com.dz.enumeration.AppHttpCodeEnum;
import com.dz.exception.SystemException;
import com.dz.result.Result;
import com.dz.service.UserAddressService;
import io.jsonwebtoken.lang.Assert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Objects;

/**
 * @author 鄧浩奇
 * @classname UserAddressController
 * @description 用户地址信息接口
 * @since 2024/4/4 16:51
 **/

@Api(tags = "用户地址接口")
@RestController
@RequestMapping("/user/addressBook")
public class UserAddressController {

    private final UserAddressService userAddressService;

    @Autowired
    public UserAddressController(UserAddressService userAddressService) {
        this.userAddressService = userAddressService;
    }

    /*
     * 新增地址
     * */
    @ApiOperation("新增地址")
    @PostMapping()
    public Result<String> addAddress(@RequestBody AddressBook addressBook) {
        Assert.notNull(addressBook, MessageConstant.DATA_ERROR);
        Long userID = BaseContext.getCurrentId();
        addressBook.setUserId(userID);
        Integer flag = userAddressService.addAddress(addressBook);
        return flag > 0 ? Result.success() : Result.error(MessageConstant.ADDRESS_BOOK_INSERT_FAILURE);
    }

    /*
     * 查询当前登陆用户所有地址信息
     * */
    @ApiOperation("查询当前登陆用户所有地址信息")
    @GetMapping("/list")
    public Result<List<AddressBook>> getAddress() {
        Long userID = BaseContext.getCurrentId();
        if (Objects.isNull(userID)) {
            throw new SystemException(AppHttpCodeEnum.ID_ERROR);
        }
        List<AddressBook> list = userAddressService.getAddressesByUserId(userID);
        return Result.success(list);
    }

    /*
     * 设置默认地址
     * */
    @ApiOperation("设置默认地址")
    @PutMapping("/default")
    public Result<String> setDefaultAddress(@RequestBody LinkedHashMap<String, Integer> addressID) {
        Assert.notNull(addressID, MessageConstant.PARAM_ERROR);
        Long userID = BaseContext.getCurrentId();
        if (Objects.isNull(userID)) {
            throw new SystemException(AppHttpCodeEnum.ID_ERROR);
        }
        Integer addressId = addressID.get("id");
        Integer flag = userAddressService.setDefaultByAddressId(userID, addressId);
        return flag > 0 ? Result.success() : Result.error(MessageConstant.UPDATE_FAILURE);

    }

    /*
     * 查询默认地址
     * */
    @ApiOperation("查询默认地址")
    @GetMapping("/default")
    public Result<AddressBook> getDefault() {
        Long userID = BaseContext.getCurrentId();
        if (Objects.isNull(userID)) {
            throw new SystemException(AppHttpCodeEnum.ID_ERROR);
        }
        AddressBook addressBook = userAddressService.getDefaultByUserId(userID);
        return Result.success(addressBook);
    }


    /*
     * 根据id查询地址
     * */
    @ApiOperation("根据id查询地址")
    @GetMapping("/{id}")
    public Result<AddressBook> getAddressById(@PathVariable Long id) {
        Assert.notNull(id, MessageConstant.PARAM_ERROR);
        AddressBook addressBook = userAddressService.getAddressById(id);
        if (Objects.isNull(addressBook)) throw new SystemException(AppHttpCodeEnum.DATA_ERROR);
        return Result.success(addressBook);
    }


    /*
     *根据id修改地址
     * */
    @ApiOperation("根据id修改地址")
    @PutMapping()
    public Result<String> updateById(@RequestBody @Validated AddressBook addressBook) {
        Assert.notNull(addressBook, MessageConstant.PARAM_ERROR);
        Integer rows = userAddressService.updateById(addressBook);
        return rows > 0 ? Result.success() : Result.error(MessageConstant.UPDATE_FAILURE);
    }

    /*
    * 根据id删除地址
    * */
    @ApiOperation("根据id删除地址")
    @DeleteMapping()
    public Result<String> deleteById(Long id) {
        Assert.notNull(id,MessageConstant.PARAM_ERROR);
        Integer rows = userAddressService.deleteById(id);
        return rows > 0 ? Result.success() : Result.error(MessageConstant.DelETE_FAILURE);
    }
}
